package com.hc.json.adapter;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/9/15 18:40
 */
public class HcJacksonAdapterConfigure implements JsonAdapterConfigure {
    @Override
    public void config(Object o) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (o instanceof ObjectMapper) {
            ObjectMapper mapper = (ObjectMapper) o;
            // 空对象
            mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
            // 空属性
            mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            // 忽略null
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            // LocalDateTime
            mapper.findAndRegisterModules();

            SimpleModule module = new SimpleModule();
            module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
            module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
            mapper.registerModule(module);
        }
        System.out.println("HcJacksonAdapterConfigure--------------------------");
    }
}
